package cn.sciento.farm.devicemanager.common.util;


import cn.sciento.farm.devicemanager.common.exception.NullDeviceTypeException;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * User: wumu
 * Date: 12/21/17
 * Time: 10:36 PM
 */
public class DeviceUtil {

    private static final int USR_LORA_CODE_LENGTH = 18;
    private static final String USR_LORA_CODE_BEGIN = "d8b0";

    private static final byte[] CHECK_BIT = new byte[]{8, 3, 7, 12, 2, 9, 5, 10, 4};
    private static final byte[] CHECK_OFFSET = new byte[]{20, 50, 28, 32, 34, 52, 36, 30, 28};

    public static String validateConCode(String number) throws NullPointerException, NumberFormatException {
        if (number == null || number.length() == 0) {
            throw new NullPointerException("number不能为空");
        }
        String originNumber = SensorBase64Util.decode(number, "utf8");
        if (originNumber == null || originNumber.length() != 24) {
            throw new NullPointerException("解码出现错误");
        }
        String concentrator = originNumber.substring(0, 15);
        String checkCode = originNumber.substring(15);
        if (!validate(concentrator, checkCode)) {
            throw new NumberFormatException("校验失败");
        }
        return concentrator;
    }

    public static String validateUsrLoraGw(String number) throws NullPointerException,IllegalArgumentException{
        if(number == null ){
            throw new NullPointerException("编号不能为空");
        }
        String smallNumber = number.toLowerCase();
        if(smallNumber.length() != USR_LORA_CODE_LENGTH || !smallNumber.startsWith(USR_LORA_CODE_BEGIN)){
            throw new IllegalArgumentException("格式不符合规格");
        }
        String concentrator = smallNumber.substring(4,12);
        return concentrator;
    }

    public static String getConCode(String con){
        if(con == null || con.length() == 0){
            throw  new NullPointerException("网关编号不能为空");
        }
        String checkCode = generateCode(con);
        return SensorBase64Util.encode(con+checkCode,"utf8");
    }

    private static String generateCode(String con) {
        byte[] conByte = con.getBytes();
        byte[] checkByte = new byte[CHECK_BIT.length];
        for (int i = 0; i < CHECK_BIT.length; i++) {
            int value = conByte[CHECK_BIT[i] - 1] + CHECK_OFFSET[i];
            checkByte[i] = (byte) value;
        }
        return new String(checkByte);
    }

    public static String validateLoraWan(String number) {
        if (StringUtils.isEmpty(number)) {
            return null;
        }
        return number.toLowerCase();
    }


    private static boolean validate(String concentrator, String checkCode) {
        if (concentrator.length() != 15 || checkCode.length() != 9) {
            return false;
        }
        byte[] checkByte = checkCode.getBytes();
        byte[] conByte = concentrator.getBytes();
        for (int i = 0; i < CHECK_BIT.length; i++) {
            int value = conByte[CHECK_BIT[i] - 1] + CHECK_OFFSET[i];
            if (value != checkByte[i]) {
                return false;
            }
        }
        return true;
    }

    public static boolean validateCon(String concentrator) {
        if (concentrator == null) {
            return false;
        }
        return true;
    }


    public static void main(String[] arg) {
        String code = "OkY4MTc3NkZwOTQxMzE4hmpNUVRDo3th";
        String concentrator = validateConCode(code);
        System.out.println(concentrator);

        String con = "869300032139226";
        System.out.println("原始："+con+" 编码后 "+getConCode(con));
    }

    public static String  handleWindDirData(float data){
        if(data < 0 || data > 360){
            return "";
        }
        if(data > 337.5 || data <= 22.5){
            return "北风";
        }else if(data > 22.5 && data <= 67.5){
            return "东北风";
        }else if(data > 67.5 && data <= 112.5){
            return "东风";
        }else if(data > 122.5 && data <= 157.5){
            return "东南风";
        }else if(data > 157.5 && data <= 202.5){
            return "南风";
        }else if(data > 202.5 && data <= 247.5){
            return "西南风";
        }else if(data > 247.5 && data <= 292.5){
            return "西风";
        }else if(data > 292.5 && data <= 337.8){
            return "西北风";
        }
        return "";
    }


//    public static float computeRealValue(String formula, int value) {
//        double realValue = 0;
//
//        if (formula == null) {
//            throw new NullDeviceTypeException("没有相关设备类型");
//        }
//        Expression compiledExp = AviatorEvaluator.compile(formula);
//        Map<String, Object> env = new HashMap<String, Object>();
//        env.put("VAL", value);
//        // 执行表达式
//        realValue = ((Number) compiledExp.execute(env)).doubleValue();
//        return NumberUtil.double2float(realValue, 2);
//    }
//
//    public static String generateUsrLora() {
//        //简单实现
//        long id = SnowFlakeUtil.generateId();
//        return id+"";
//    }
}
